package org.example.leetCode;

import com.sun.deploy.util.StringUtils;
import com.sun.org.apache.regexp.internal.RE;

/**
 * @Author Qinkai
 * @Description TODO $
 * @Date $ 2019-09-18$
 * @Param $
 * @return $
 **/
public class RomanToInt {
    public int romanToInt(String s) {
        if ("".equals(s)) {
            return 0;
        }
        char[] chars = s.toCharArray();
        int num = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == 'M') {
                num += 1000;
                continue;
            }
            if (chars[i] == 'D') {
                num += 500;
                continue;
            }
            if (chars[i] == 'C') {
                if (i + 1 < chars.length && chars[i + 1] == 'M') {
                    num += 900;
                    i++;
                } else if (i + 1 < chars.length && chars[i + 1] == 'D') {
                    num += 400;
                    i++;
                } else {
                    num += 100;
                }
                continue;
            }
            if (chars[i] == 'L') {
                num += 50;
                continue;
            }
            if (chars[i] == 'X') {
                if (i + 1 < chars.length && chars[i + 1] == 'C') {
                    num += 90;
                    i++;
                } else if (i + 1 < chars.length && chars[i + 1] == 'L') {
                    num += 40;
                    i++;
                } else {
                    num += 10;
                }
                continue;
            }
            if (chars[i] == 'V') {
                num += 5;
                continue;
            }
            if (chars[i] == 'I') {
                if (i + 1 < chars.length && chars[i + 1] == 'V') {
                    num += 4;
                    i++;
                } else if (i + 1 < chars.length && chars[i + 1] == 'X') {
                    num += 9;
                    i++;
                } else {
                    num += 1;
                }
            }
        }
        return num;
    }
}
